<template>
    <view class="goods-add">
        <view class="tit-box" @mousedown="mousedown">
            <view class="add-g-title">{{
                actType == 1 || actType == 4
                    ? "新增产品"
                    : actType == 3
                    ? "复制产品"
                    : "编辑产品"
            }}</view>
            <minMaxClose></minMaxClose>
        </view>

        <view class="info-box comm-list-scroll">
            <!-- 图片 -->
            <addPhoto
                ref="goodsAddPhoto"
                @showSkuBindImg="showSkuBindImg"
                :isSys="isSys"
            ></addPhoto>
            <!-- 基础信息 -->
            <view class="info-base">
                <inputTxt
                    ref="gdTitle"
                    :title="'产品品名'"
                    :otherTxt="', 50字以内 (必填)'"
                    :value="title"
                    :isMust="true"
                    :maxlength="50"
                    @input="inputTit"
                    v-show="limit.title"
                ></inputTxt>
                <inputTxt
                    ref="gdNo"
                    :title="'产品货号'"
                    :creatTxt="'生成'"
                    :otherTxt="', 20字以内 (必填)'"
                    :value="item_no"
                    :isMust="true"
                    :maxlength="18"
                    @input="inputGdsNo"
                    v-show="limit.item_no"
                ></inputTxt>
                <inputTxt
                    ref="gdBarCode"
                    :title="'产品条码'"
                    :otherTxt="', 支持多个 (空格或逗号隔开)'"
                    :value="barcode"
                    @input="inputGdBarCode"
                    v-show="limit.barcode"
                ></inputTxt>
            </view>

            <view class="more-box" v-show="sz">
                <!-- 规格 -->

                <levelGroup
                    class="s-l-grp"
                    ref="spec"
                    :type="1"
                    @changeSpec="changeSpecUnit"
                    :spec="limit.spec"
                ></levelGroup>
                <!-- 单位 -->
                <unitGroup
                    ref="unit"
                    :type="1"
                    @changeUnitPriceCover="changeUnitPriceCover"
                    @changeUnit="changeSpecUnit"
                    :unit="limit.unit"
                ></unitGroup>
                <!-- 售价 -->
                <sellingPrice
                    :is_price_quote_type="limit.is_price_quote_type"
                    ref="price"
                    :title="'售价'"
                    :titleTxt="'快速填充'"
                ></sellingPrice>
                <!-- 成本 -->
                <sellingCost
                    ref="cost"
                    :title="'成本'"
                    :titleTxt="'快速填充'"
                    v-show="limit.cost"
                ></sellingCost>
                <!-- 类别-->
                <levelGroup class="s-l-grp" ref="cat" :type="2"></levelGroup>
                <!-- 品牌 -->
                <goodsBrand ref="brand" v-show="limit.brand"></goodsBrand>
                <!-- 标签-->
                <goodsTag ref="tag" v-show="limit.tag"></goodsTag>
                <!-- 库存 -->
                <SellingStore
                    ref="store"
                    :title="'初始库存'"
                    v-show="limit.isqty"
                ></SellingStore>
                <!-- 警戒库存 -->
                <inputTxt
                    ref="warnQtyNum"
                    :title="'警戒库存'"
                    :otherTxt="'数量'"
                    :value="qty_warn"
                    @input="inputWarnQty"
                    :type="'number'"
                ></inputTxt>
                <!-- 起订量 -->
                <inputTxt
                    ref="buyMinNum"
                    :title="'起订数量'"
                    :value="buymin"
                    @input="inputBuymin"
                    :type="'number'"
                ></inputTxt>
                <!-- 分享描述 -->
                <inputTxt
                    ref="gdShareTxt"
                    :title="'产品分享描述'"
                    :otherTxt="', 25字以内'"
                    :value="txt_share"
                    :maxlength="25"
                    @input="inputShare"
                    v-show="limit.share"
                ></inputTxt>
                <!-- 上架 -->
                <goodsPutShelf
                    ref="shelf"
                    v-show="limit.onsale"
                ></goodsPutShelf>
                <!-- 经手人 -->
                <dealer ref="dealer" v-show="limit.handler"></dealer>

                <!-- 供货单位 -->
                <supplierCom
                    ref="supplierCom"
                    :suupInfo="suup_info"
                    v-if="limit.supplier"
                ></supplierCom>

                <!-- 备注 -->
                <remarks
                    ref="remarks"
                    :value="comment"
                    v-show="limit.txt"
                ></remarks>
                <!-- 更多详情 -->
                <goodsMoreDetail
                    ref="moreDetail"
                    v-show="limit.desc"
                ></goodsMoreDetail>
            </view>
            <view class="sz-btn" @click="szOnclick">{{
                sz ? "收起全部详情" : "展开全部详情"
            }}</view>
        </view>

        <view class="a-d-btn-box">
            <!-- <view class="c-btn">清除</view> -->
            <view class="f-btn" @click="onSaveGds">保存</view>
        </view>
        <!-- 编辑图片 -->
        <imageCropper
            ref="imageCropper"
            :src="editUrl"
            :curEditType="curEditType"
            @confirm="confirm"
            @cancel="cancel"
        >
        </imageCropper>

        <popSkuBindImg
            ref="popSkuBindImg"
            @setSkuImg="setSkuImg"
        ></popSkuBindImg>
    </view>
</template>

<script>
import minMaxClose from "../min-max-close.vue";

import windBtnContrl from "../../js/WinBtnContrl.js";

import addPhoto from "../comm/AddPhoto.vue";

import inputTxt from "../comm/InputTxt.vue";

import levelGroup from "../comm/LevelGroup.vue";

import unitGroup from "../comm/UnitGroup.vue";

import sellingPrice from "./SellingPrice.vue";

import sellingCost from "./SellingCost.vue";

import SellingStore from "./SellingStore.vue";

import goodsBrand from "./GoodsBrand.vue";

import goodsTag from "./GoodsTag.vue";

import goodsShareDisc from "./GoodsShareDisc.vue";

import goodsPutShelf from "./GoodsPutShelf.vue";

import dealer from "../comm/Dealer.vue";

import remarks from "../comm/Remarks.vue";

import goodsMoreDetail from "./GoodsMoreDetail.vue";

import supplierCom from "./supplierCom.vue";

import popSkuBindImg from "../comm/PopSkuBindImg.vue";

import goodsInfoBean from "../../js/objectValue/GoodsInfoBean.js";

import eventCtrl from "../../js/event/EventCtrl.js";

import goodsCtrl from "../../js/gd/GoodsCtrl.js";

import accessCtrl from "../../js/limit/AccessCtrl.js";

import imageCropper from "../comm/ImageCropper.vue";

import staticConst from "../../js/const/StaticConst.js";

import goodsTlpCtrl from "../../js/gd/GoodsTlpCtrl.js";

import localStore from "@/js/LocalStore.js";

export default {
    props: ["actType"],
    components: {
        minMaxClose,
        addPhoto,
        inputTxt,
        levelGroup,
        unitGroup,
        sellingPrice,
        sellingCost,
        SellingStore,
        goodsBrand,
        goodsTag,
        goodsShareDisc,
        goodsPutShelf,
        dealer,
        remarks,
        goodsMoreDetail,
        imageCropper,
        popSkuBindImg,
        supplierCom,
    },
    data() {
        return {
            editUrl: "", //编辑图片地址
            sz: false,

            title: "", //产品名称
            item_no: "", //产品货号
            barcode: "", //产品条码
            buymin: "", //起订量
            qty_warn: "", //警戒库存
            comment: "", //备注信息
            txt_share: "", //分享描述
            curEditType: -1, //当前编辑的 1：产品列表，2：产品详情
            parentIdx: -1, //当前编辑的图文索引,
            photoIdx: -1, //图文块图片索引
            // 供货商信息
            suup_info: {
                supp_name: "", // 供货商名称
                supp_id: "", // 供货商id
            },
            limit: {
                barcode: false,
                brand: false,
                cost: false,
                desc: false,
                spec: false,
                item_no: true,
                onsale: false,
                share: false,
                tag: false,
                title: true,
                txt: false,
                unit: false,
                isqty: false,
                handler: false,
                supplier: false,
                is_price_quote_type: false,
            },

            isSys: false,
        };
    },

    methods: {
        /*****显示规格单位 sku 选择******/

        showSkuBindImg(data) {
			console.log(data,'23333333')
            this.$refs.popSkuBindImg.show("show", data);
        },

        setSkuImg(data) {
            let res = goodsCtrl.sanfCheckSkuForPhoto(data);
            console.log(res,'-----23333333')
            this.$refs.goodsAddPhoto.setPhotoArr(res);
        },

        /**移动窗口***/
        mousedown() {
            windBtnContrl.mousedown();
        },

        /****收起，展开****/
        szOnclick() {
            this.sz = !this.sz;
        },

        /****初始化数据****/
        async init() {
            let cp = await localStore.getCpInfo();
            this.isSys = cp.is_sys == "1" ? true : false;

            this.removeAndAddEvent();
            let goods = goodsCtrl.getGoodsInfoBean();
            if (goods) {
                this.setInputsData(goods);
            } else {
                this.clearPageData();
                if (goodsCtrl.keyCode != "") {
                    setTimeout(this.setGdBarCode, 100, goodsCtrl.keyCode); //渲染问题
                    // this.setGdBarCode(goodsCtrl.keyCode);
                }
            }

            this.setPhoto(); //产品图片
            // this.setInputsData(goods);
            this.setSpecData();
            this.setUnitData();
            this.setPriceData();
            this.setCostData();
            this.setBrandData();
            this.setCatData();
            this.setTagData();
            this.setStoreData();
            this.setDealerData();
            this.setShelfrData();
            this.setMoreDetail();
            this.initLimit();
        },

        setGdBarCode(keyCode) {
            this.barcode = keyCode;
            this.$refs.gdBarCode.setBarcode(keyCode);
            goodsCtrl.keyCode = "";
        },

        /*****清除数据*******/
        clearPageData() {
            this.title = "";
            this.item_no = "";
            this.barcode = "";
            this.buymin = "";
            this.qty_warn = "";
            this.comment = "";
            this.txt_share = "";
            this.suup_info.supp_name = "";
            this.suup_info.suppid = "";
            this.$nextTick(() => {
                if (this.$refs.gdTitle) this.$refs.gdTitle.clear();
                if (this.$refs.goodsAddPhoto) this.$refs.goodsAddPhoto.clear();
                if (this.$refs.gdNo) this.$refs.gdNo.clear();
                if (this.$refs.gdBarCode) this.$refs.gdBarCode.clear();
                if (this.$refs.warnQtyNum) this.$refs.warnQtyNum.clear();
                if (this.$refs.buyMinNum) this.$refs.buyMinNum.clear();
                if (this.$refs.gdShareTxt) this.$refs.gdShareTxt.clear();
            });
        },

        async initLimit() {
            this.limit.barcode = await goodsTlpCtrl.getTplBarCodeLimit();
            this.limit.brand = await goodsTlpCtrl.getTplBrandLimit();
            this.limit.cost =(await accessCtrl.getGdsCostSeeLimit()) && (await goodsTlpCtrl.getTplCostLimit());
			this.limit.is_price_quote_type = await goodsTlpCtrl.getPriceQuoteType();
			let moreDeTabLsLen = this.$refs.moreDetail.tabList.length;
            this.limit.desc = await goodsTlpCtrl.getTplDescLimit();
			if(this.limit.desc && moreDeTabLsLen == 0)
			{
				this.limit.desc = false;
			}
			
            this.limit.spec = await goodsTlpCtrl.getTplSpecLimit();
            this.limit.item_no = await goodsTlpCtrl.getTplItemNoLimit();
            this.limit.onsale =
                (await accessCtrl.getGdsOnSale()) &&
                (await goodsTlpCtrl.getTplOnSaleLimit());
            this.limit.share = await goodsTlpCtrl.getTplShareLimit();
            this.limit.tag =
                (await accessCtrl.getGdsTgsLimit()) &&
                (await goodsTlpCtrl.getTplTagLimit());
            this.limit.title = await goodsTlpCtrl.getTplTitleLimit();
            this.limit.txt = await goodsTlpCtrl.getTplTxtLimit();
            this.limit.unit = await goodsTlpCtrl.getTplUnitLimit();
            this.limit.isqty = await goodsCtrl.getShowQtyLimit(); //是否可以编辑库存
            this.limit.handler = await accessCtrl.getGdsManagerChangeLimit(); //
            this.limit.supplier = await accessCtrl.getPurchLimit();
        },

        /****设置图片*****/
        setPhoto() {
            if (
                this.$refs.goodsAddPhoto &&
                this.$refs.goodsAddPhoto.initPhotoData
            ) {
                this.$refs.goodsAddPhoto.initPhotoData();
            }
        },

        /*******输入产品名称******/
        inputTit(value) {
            goodsCtrl.setGoodsName(value);
        },

        /*******输入产品货号**********/
        inputGdsNo(value) {
            goodsCtrl.setGoodsNo(value);
        },

        /*********输入条码**************/
        inputGdBarCode(value) {
            goodsCtrl.setGoodsBarCode(value);
        },

        /***初始化基础数据***/
        setInputsData(goods) {
            console.log("产品信息", goods);
            this.title = goods.title;
            this.item_no = goods.item_no;
            this.barcode = goods.barcode;
            this.buymin = goods.buymin;
            this.qty_warn = goods.qty_warn;
            this.comment = goods.comment;
            this.txt_share = goods.txt_share;
            this.suup_info.supp_name = goods.supp_name;
            this.suup_info.suppid = goods.suppid;
        },
        /***初始化规格***/
        setSpecData() {
            if (this.$refs.spec && this.$refs.spec.initData)
                this.$refs.spec.initData();
        },

        /****初始化单位***/
        setUnitData() {
            if (this.$refs.unit && this.$refs.unit.initData)
                this.$refs.unit.initData();
        },

        /***初始化价格**/
        setPriceData() {
            if (this.$refs.price && this.$refs.price.initData) {
                this.$refs.price.initData();
            }
        },

        /*****初始化成本******/
        setCostData() {
            if (this.$refs.cost && this.$refs.cost.initData)
                this.$refs.cost.initData();
        },

        /*****初始化品牌******/
        setBrandData() {
            if (this.$refs.brand && this.$refs.brand.initData)
                this.$refs.brand.initData();
        },

        /*****初始化类别******/
        setCatData() {
            if (this.$refs.cat && this.$refs.cat.initData)
                this.$refs.cat.initData(2);
        },

        /****初始初始标签*****/
        setTagData() {
            if (this.$refs.tag && this.$refs.tag.initData)
                this.$refs.tag.initData();
        },

        /****初始初始库存*****/
        setStoreData() {
            if (this.$refs.store && this.$refs.store.initData)
                this.$refs.store.initData();
        },

        /***** 初始化经手人 *****/
        setDealerData() {
            if (this.$refs.dealer && this.$refs.dealer.initData)
                this.$refs.dealer.initData();
        },

        /***** 初始化上架 *****/
        setShelfrData() {
            if (this.$refs.shelf && this.$refs.shelf.initData)
                this.$refs.shelf.initData();
        },

        /***** 初始化更多详情 *****/
        setMoreDetail() {
            if (this.$refs.moreDetail && this.$refs.moreDetail.initData)
			{
                this.$refs.moreDetail.initData();
				
				
			}
        },

        /********改变了规格,单位****2:规格，1：单位*****/
        changeSpecUnit(type) {
            this.setPriceData();
            this.setCostData();
            this.setStoreData();
            this.setSpecUintBindPhoto(type);
        },
		
		changeUnitPriceCover(data)
		{
			this.$refs.price.changePriceEvent(data);
		},
		
        /****设置规格单位图片***/
        setSpecUintBindPhoto(type) {
            goodsCtrl.changeSpecUnitForPhoto(type);
        },

        /*****确认编辑图片****/
        confirm(data) {
            console.log("ddd==xxx==",data)
            if (this.curEditType == staticConst.UPLOAD_GOODS)
                this.$refs.goodsAddPhoto.confirm(data);
            else if (this.curEditType == staticConst.UPLOAD_OTHER) {
                data.parentIdx = this.parentIdx;
                data.photoIdx = this.photoIdx;
                this.$refs.moreDetail.confirm(data);
            }
        },

        /****取消编辑图片*****/
        cancel() {
            if (this.curEditType == staticConst.UPLOAD_GOODS)
                this.$refs.goodsAddPhoto.cancel();
            else if (this.curEditType == staticConst.UPLOAD_OTHER)
                this.$refs.moreDetail.cancel();
        },

        /***编辑图片***/
        editPhoto(data) {
            this.editUrl = data.url;
            this.curEditType = data.type;
            if (this.curEditType == staticConst.UPLOAD_OTHER) {
                this.parentIdx = data.parentIdx;
                this.photoIdx = data.index;
            }
        },

        /****产品分享文字*****/
        inputShare(value) {
            goodsCtrl.inputShareTxt(value);
        },

        /***产品警告库存****/
        inputWarnQty(value) {
            goodsCtrl.inputWarnQty(value);
        },

        /**最小起订量**/
        inputBuymin(value) {
            goodsCtrl.inputBuymin(value);
        },

        /****保存产品****/
        onSaveGds() {
            goodsCtrl.saveGoods();
        },

        /***注册事件*****/
        addEvent() {
            uni.$on(eventCtrl.SHOW_ADD_GOODS_EVENT, this.init); //添加产品
            uni.$on(eventCtrl.EDIT_PHOTO_EVENT, this.editPhoto); //编辑图片
        },

        /***删除事件*****/
        removeEvent() {
            uni.$off(eventCtrl.SHOW_ADD_GOODS_EVENT); //删除
            uni.$off(eventCtrl.EDIT_PHOTO_EVENT); //编辑图片
        },

        /****注册删除事件*****/
        removeAndAddEvent() {
            this.removeEvent();
            this.addEvent();
        },
    },

    mounted() {
        this.addEvent();
    },
};
</script>
<style lang="scss">
.goods-add {
    flex: 1;
    margin-left: 345px;
    height: 100vh;
    background-color: #fff;
    // box-sizing: border-box;
    .tit-box {
        border-bottom: 1px #ececec solid;
        position: relative;
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        height: 70px;
        .add-g-title {
            font-size: 18px;
            padding-left: 20px;
            color: #333;
        }
    }

    .info-box {
        height: calc(100vh - 131px);
        padding-left: 20px;
        padding-right: 20px;
        border-bottom: 1px #ececec solid;
        .info-base {
            margin-bottom: 20px;
        }
        .more-box {
            width: 100%;
            .s-l-grp {
                width: 100%;
            }
        }
        .sz-btn {
            line-height: 40px;
            margin-left: 50px;
            margin-right: 50px;
            margin-bottom: 20px;
            margin-top: 20px;
            border: 1px #3388ff solid;
            color: #3388ff;
            text-align: center;
            font-size: 16px;
            border-radius: 8px;
            cursor: pointer;
        }
    }
    .a-d-btn-box {
        display: flex;
        height: 60px;
        width: 100%;
        bottom: 0;
        text-align: center;

        font-size: 16px;
        line-height: 60px;
        position: relative;
        .c-btn {
            color: #333;
            background-color: #fff;
            width: 100px;
            height: 100%;
            position: absolute;
            right: 200px;
        }
        .c-btn:hover {
            opacity: 0.9;
        }

        .f-btn {
            color: #fff;
            background-color: #3388ff;
            width: 200px;
            height: 60px;
            position: absolute;
            right: 0;
            cursor: pointer;
        }

        .f-btn:hover {
            opacity: 0.9;
        }
    }
}
</style>